package com.lab.tools.ui;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.TextView;

import com.cuitrip.service.R;
import com.lab.component.list.viewholder.CTRecycleViewHolder;
import com.lab.jumper.FragmentJumper;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ak on 15/11/24.
 */
public class LogView extends CTRecycleViewHolder<HttpLog> {

    public static final int REQUEST_TYPE = 0x01;
    public static final int RESPONSE_TYPE = 0x02;
    @Bind(R.id.api_name)
    TextView itemText;
    @Bind(R.id.status_code)
    TextView statusCodeText;
    private HttpLog httpLog;
    private FragmentActivity context;

    public LogView(View itemView, FragmentActivity context) {
        super(itemView);
        this.context = context;
        ButterKnife.bind(this, itemView);
    }

    public void render(final HttpLog httpLog, int position) {
        this.httpLog = httpLog;
        itemText.setText(httpLog.getApiName());
        statusCodeText.setText(String.valueOf(httpLog.getCode()));

    }

    @OnClick(R.id.request_body)
    void requestBody() {
        showContent(REQUEST_TYPE);
    }

    @OnClick(R.id.response_body)
    void responseBody() {
        showContent(RESPONSE_TYPE);

    }

    void showContent(int type) {
        HttpContent httpContent;
        if (type == REQUEST_TYPE) {
            httpContent = new HttpContent(httpLog.getApiName(), httpLog.getCode(), httpLog.getRequest());
        } else {
            httpContent = new HttpContent(httpLog.getApiName(), httpLog.getCode(), httpLog.getResponse());
        }
        LogDetailFragment logDetailFragment = new LogDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(LogDetailFragment.DATA_KEY, httpContent);
        logDetailFragment.setArguments(bundle);
        FragmentJumper.addFragment(context.getSupportFragmentManager(), R.id.container, logDetailFragment, LogDetailFragment.class.getName(), true);
    }


}

